/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.entities;

import java.io.Serializable;
import javax.persistence.*;
import org.jdom.Element;
import ru.ssau.utils.XMLHelper;


@Entity
@Table(name="AuthorizationInfo")
public class AuthorizationInfo implements Serializable {
    
    @Id
    @Column(name="AuthorizationID", nullable=false)
    @SequenceGenerator(name="AuthInfoIdGenerator", sequenceName="AuthInfoIDSeq", allocationSize=1)
    @GeneratedValue(generator="AuthInfoIdGenerator")
    private Integer id;

    @Column(length=20, unique=true, nullable=false)
    private String login;

    @Column(name="Pass", length=20, nullable=false)
    private String password;

    @OneToOne
    @JoinColumn(name="UserID")
    private User user;

    public AuthorizationInfo() {

    }

    public AuthorizationInfo(String login, String password) {
        this.login = login;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(obj == this) {
            return true;
        }
        if(!(obj instanceof AuthorizationInfo)) {
            return false;
        }
        AuthorizationInfo other = (AuthorizationInfo) obj;
        return id.equals(other.id) && login.equals(other.login) &&
                password.equals(other.password);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 47 * hash + (this.login != null ? this.login.hashCode() : 0);
        hash = 47 * hash + (this.password != null ? this.password.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", login=");
        sb.append(login);
        sb.append(", password=");
        sb.append(password);
        sb.append("]");
        return sb.toString();
    }

    public Element toXMLElement() {
        Element root = new Element("AuthorizationInfo");
        XMLHelper.addElement(root, "id", id);
        XMLHelper.addElement(root, "login", login);
        XMLHelper.addElement(root, "password", password);
        return root;
    }

}
